package com.randomrpg.game

import Math._

object Magics {
  
  val magicCost = 10
  private def magicDamage(player: Player) = player.int * 2 + player.level
  
  /**
   * Heal player by will * 2
   */
  def heal(player: Player): Int = {
    if(player.clazz.isInstanceOf[Cleric] && player.mp > magicCost) {
      val oldLife = player.life
      player.life = giveLife(player, player.will * 2)
      player.mp = burnMp(player)
      player.life - oldLife
    } else 0
  }

  /**
   * Cast fireball on enemy
   * @param player caster
   * @param enemy victim
   * @return damage done
   */
  def fireball(player: Player, enemy: Player): Int = {
    if(player.clazz.isInstanceOf[Mage] && player.mp > magicCost) {
      val oldLife = enemy.life
      enemy.life -= magicDamage(player)
      player.mp = burnMp(player)
      oldLife - enemy.life
    } else 0
  }
  
  def drain(player: Player, enemy: Player): (Int, Int) = {
    if(player.clazz.isInstanceOf[Mage] && player.mp > magicCost) {
      val oldLife = enemy.life
      enemy.life -= magicDamage(player) / 2
      val myOldLife = player.life
      player.life = giveLife(player, (magicDamage(player) / 2))
      player.mp = burnMp(player)
      (oldLife - enemy.life, player.life - myOldLife)
    } else (0, 0)
  }
  
  private def giveLife(player: Player, amount: Int): Int = {
    min(player.maxLife, player.life + amount)
  }
  
  private def burnMp(player: Player): Int = {
    max(0, player.mp - magicCost)
  }
}